package com.atguigu.mock.entity;

import com.atguigu.common.base.entity.AutoIdBase;
import com.atguigu.common.comment.Comment;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

@Entity
@Getter
@Setter
@NoArgsConstructor
@Comment("套餐表")
public class ProductGroup extends AutoIdBase {
    @ManyToMany
    @JoinTable(name = "product_group_sku",
            joinColumns = @JoinColumn(name = "product_group_id"),
            inverseJoinColumns = @JoinColumn(name = "product_sku_id")
    )
    private List<ProductSku> productSkuList;

    @Comment("套餐名称")
    private String name;
    @Comment("套餐原价")
    private BigDecimal originalPrice;
    @Comment("套餐价格")
    private BigDecimal price;

    public ProductGroup(LocalDateTime createTime, List<ProductSku> productSkuList, String name) {
        super(createTime);
        this.productSkuList = productSkuList;
        this.originalPrice = productSkuList.stream().map(ProductSku::getPrice).reduce(new BigDecimal(0), BigDecimal::add);
        BigInteger bigInteger = this.originalPrice.multiply(new BigDecimal(Double.toString(0.6 + Math.random() * 0.2))).toBigInteger();
        this.price = new BigDecimal(bigInteger).subtract(new BigDecimal("0.01"));
        this.name = name;
    }
}
